app.controller('AddNetworkModalCtrl', ['$rootScope', '$scope', '$stateParams', '$state', 'httpLoad', 'CommonData',
	function ($rootScope, $scope, $stateParams, $state, httpLoad, CommonData) {
		$rootScope.link = '/statics/css/tpl-card.css';//引入页面样式
		$scope.environmentData = CommonData.environmentData;
		$scope.typeData = CommonData.networkType;
		$scope.targetCategoryName = 'Switch';
		sessionStorage.setItem('networktabLocation', '1');
		//初始化默认值
		$scope.itemData = {
			type: 0,
			environment: 'DEVELOP'
		};
		var url = '/device/network/create';
		var initDetailCreate = function () {
			//获取数据中心列表数据
			httpLoad.loadData({
				url: '/dc/list',
				noParam: true,
				success: function (data) {
					if (data.success) {
						$scope.dataCenterData = data.data.rows;
					}
				}
			});
		};
		// 如果为编辑，进行赋值
		var initDetailEdit = function (id) {
			url = '/device/network/modify';
			httpLoad.loadData({
				url: '/device/network/detail',
				method: 'GET',
				data: {id: id},
				success: function (data) {
					if (data.success) {
						$scope.itemData = data.data;
						if ($scope.itemData.serviceStart) {
							$scope.date = $scope.itemData.serviceStart + ' - ' + $scope.itemData.serviceEnd;
						}
						$scope.nodeData = {
							targetName: $scope.itemData.name,
							targetCategory: data.data.type,
							relations: $scope.itemData.resourceRelations
						};
						$scope.isDataLoad = true;
						//加载选择数据
						var loadObj = [$scope.itemData.dcId, $scope.itemData.roomId];
						loadObj.forEach(function(item,index){
							$scope.loadSelectData(index,item, 1);
						});
					}
				}
			});
		};
		$scope.relations = function (value) {		
			httpLoad.loadData({
				url: '/res/rule/relations',			
				data: {
					category: value				
				},
				success: function (data) {
					if (data.success) {
						$scope.typeDataone = data.data;
					}
				}
			})
		};
		
		(function () {
			switch ($stateParams.flag / 1) {
				case 4:
					$rootScope.moduleTitle = '资源管理 > 添加网络设备';
					$scope.modalName = '添加网络设备';
					$scope.sourceDisabled = false;
					initDetailCreate();
					break;
				case 5:
					$rootScope.moduleTitle = '资源管理 > 编辑网络设备';
					$scope.modalName = '编辑网络设备';
					initDetailCreate();
					initDetailEdit($stateParams.id);
					break;
			}
		})();
		//四级联动加载数据
		$scope.loadSelectData = function (level, value, flag) {//flag用来判断编辑还是选择，编辑时不清空id
			if(!value) return;
			var loadRoom = function () {
				var param = [{param: {dcId: value}, sign: 'EQ'}];
				httpLoad.loadData({
					url: '/room/list',
					noParam: true,
					data: {
						simple: true,
						params: JSON.stringify(param)
					},
					success: function (data) {
						if (data.success) {
							if(!flag){
								$scope.cabinetData = [];
								$scope.itemData.roomId = '';
								$scope.itemData.rackId = '';
							}
							$scope.roomData = data.data.rows;
						}
					}
				})
			};
			var loadRack = function () {
				var param = [{param: {roomId: value}, sign: 'EQ'}];
				httpLoad.loadData({
					url: '/rack/list',
					noParam: true,
					data: {
						simple: true,
						params: JSON.stringify(param)
					},
					success: function (data) {
						if (data.success) {
							$scope.cabinetData = data.data.rows;
							if(!flag){
								$scope.itemData.rackId = '';
							}
						}
					}
				})
			};
			switch (level / 1) {
				case 0:
					loadRoom();
					break;
				case 1:
					loadRack();
					break;
			}
		};
		$scope.showPassword = function (value) {
			$scope[value] = !$scope[value];
		}
		var toFormatTime = function (time, place) {
			if (!time) return "";
			var date = time.split(' - ');
			return date[place / 1];
		}
		//保存按钮
		$scope.save = function () {
			//$scope.itemData.resourceRelations = $scope.spider.Adddata();
			if ($scope.date) {
				$scope.itemData.serviceStart = toFormatTime($scope.date, 0);
				$scope.itemData.serviceEnd = toFormatTime($scope.date, 1);
			}
			httpLoad.loadData({
				url: url,
				data: $scope.itemData,
				success: function (data) {
					if (data.success) {
						$scope.pop($stateParams.id ? '网络设备编辑成功' : '网络设备添加成功');
						$scope.goBack();
					}
				}
			});
		}
		$scope.goBack = function () {
			history.go(-1);
		};
	}
]);